package com.sgchen.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.sgchen.demo.entity.po.AccountPo;
import com.sgchen.demo.entity.po.ChildAccountPo;
import com.sgchen.demo.mapper.AccountMapper;
import com.sgchen.demo.service.AccountService;
import com.sgchen.security.annotation.DesensitizedMethod;
import com.sgchen.security.annotation.DesensitizedUnit;
import com.sgchen.security.enums.DesensitizedMode;
import com.sgchen.security.query.EncryptLambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sgchen
 * @date 2024/2/5 16:33
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Override
    public void addAccount(AccountPo accountPo) {
        accountMapper.insert(accountPo);
    }

    @Override
    @DesensitizedMethod({
            @DesensitizedUnit(field = "account", mode = DesensitizedMode.mobile)
    })
    public List<AccountPo> queryAllWrapper() {
        return accountMapper.selectList(Wrappers.emptyWrapper());
    }

    @Override
    public void updatePasswordByAccount(String account, String password) {
        accountMapper.updatePasswordByAccount(account, password);
    }

    @Override
    public void updateByIdWrapper(ChildAccountPo accountPo) {
        accountMapper.updateById(accountPo);
    }

    @Override
    public List<AccountPo> getInfoByMapperParams(AccountPo accountPo) {
        return accountMapper.getInfoByAccount(accountPo.getAccount());
    }

    @Override
    public List<AccountPo> getInfoByWrapper(AccountPo accountPo) {
        EncryptLambdaQueryWrapper<AccountPo> queryWrapper = new EncryptLambdaQueryWrapper<>();
        queryWrapper.eq(AccountPo::getAccount, accountPo.getAccount());
        return accountMapper.selectList(queryWrapper);
    }

    @Override
    public List<ChildAccountPo> listByMapperEntity(AccountPo accountPo) {
        return accountMapper.listByFilter(accountPo);
    }

    @Override
    @DesensitizedMethod({
            @DesensitizedUnit(field = "account", mode = DesensitizedMode.mobile)
    })
    public AccountPo infoByWrapperId(Integer id) {
        return accountMapper.selectById(id);
    }

    @Override
    public void updateByIdUpdateWrapper(AccountPo accountPo) {
        LambdaUpdateWrapper<AccountPo> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AccountPo::getId, accountPo.getId());
        queryWrapper.set(AccountPo::getEmail,accountPo.getEmail());
        queryWrapper.set(AccountPo::getAccount,accountPo.getAccount());
        accountMapper.update(null,queryWrapper);
    }

    @Override
    public void updateUpdateWrapper(AccountPo accountPo) {
        EncryptLambdaQueryWrapper<AccountPo> queryWrapper = new EncryptLambdaQueryWrapper<>();
        queryWrapper.eq(AccountPo::getId, accountPo.getId());
        accountMapper.update(accountPo,queryWrapper);
    }
}
